#include"progress.h"

#define NUM 101
#define STYLE '='

//进度条初级版本
void progress_v1()
{
    char buffer[NUM];
    memset(buffer, 0, sizeof(buffer));
    const char *lable="|/-\\";
    int len = strlen(lable);

    int cnt = 0;
    while(cnt <= 100)
    {
        printf("[%-100s][%d%%][%c]\r",buffer, cnt, lable[cnt%len]);
        fflush(stdout);
        buffer[cnt] = STYLE;
        cnt++;
        usleep(50000);
    }
    printf("\n");
}

//刷新进度条
void FlushProgress(double total, double current)
{
    char buffer[NUM];
    memset(buffer, 0, sizeof(buffer));
    const char *lable = "|/-\\";
    int len = strlen(lable);

    static int cnt = 0;//旋转标签下标

    int num = (int)(current*100/total);
    int i = 0;
    for(; i < num; i++)
    {
        buffer[i] = STYLE;
    }

    double rate = current/total;
    cnt %= len;//循环旋转
    printf("[%-100s][%.1lf%%][%c]\r", buffer, rate*100, lable[cnt]);
    cnt++;//每次次数加1
    fflush(stdout);
}
